<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../js/vue.js"></script>
    <style>
        input{
            width: 141px;
            height: 28px;
            margin-top: 30px;
        }

    </style>
</head>
<body>
    <div id="root">
        <input type="text" placeholder="请输入关键字查找" v-model="inputValue">
        <ul>
            <li v-for='p in filterPeople' :key="p.id">{{p.name}}-{{p.age}}</li>
        </ul>
    </div>
    <script>
        Vue.config.productionTip = false;
        let vm = new Vue({
            el: '#root', 
            data: {
                peoples:[
                    {id:01,name:'马冬梅',age:18},
                    {id:02,name:'周冬雨',age:19},
                    {id:03,name:'周杰伦',age:20},
                    {id:04,name:'温兆伦',age:21},
                ],
                filterPeople:[],
                inputValue:'',
            },
            watch: {
                inputValue:{
                    // 立即执行一次
                    immediate:true,
                    handler(){
                        this.filterPeople = this.peoples.filter((p) => {
                            // 设置过滤的规则(如果return的是true，则返回当前对象，最终返回一个数组)
                            return p.name.includes(this.inputValue);
                        });
                    }
                }
            }
        });
    </script>
</body>
</html>